package com.xbdun.dao.bmp.provider;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class PowerUserMenuMapperProvider {

	//根据部门角色删除菜单
	public String delOrgUserByMenuSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		List<Long> menus = (List<Long>)params.get("menus");//菜单
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("DELETE FROM xbdun_power_user_menu WHERE orgCode = #{orgCode} AND orleDepartId IN (");
		sbSql.append("SELECT b.id FROM xbdun_power_role_depart b WHERE b.orgCode =#{orgCode} AND b.orleDepartId = #{orleDepartId} AND b.code IN(");
		boolean bl = false;
		for(Long menuId:menus){
			if(bl){sbSql.append(",");}
			sbSql.append(menuId);
			if(!bl)bl = !bl;
		}
		sbSql.append("))");
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;

		return content;
	}
	
	//新增机构用户权限
	public String addOrgUserMenuSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Set<Long> roleDptIds = (Set<Long>)params.get("roleDptIds");//菜单
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("INSERT INTO xbdun_power_user_menu(userCode,orgCode,code,orleDepartId) VALUES");
		boolean bl = false;
		for(Long orleDptId : roleDptIds){
			if(bl){sbSql.append(",");}
			
			sbSql.append("(#{userCode},#{orgCode},#{departCode},"+orleDptId+")");
			if(!bl){bl=!bl;}
		}
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		roleDptIds.clear();
		return content;
	}
	
	//删除机构关联用户的部门角色权限
	public String delOrgUserMenuSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Set<Long> roleDptIds = (Set<Long>)params.get("roleDptIds");//菜单
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("DELETE FROM xbdun_power_user_menu WHERE orgCode=#{orgCode} AND userCode = #{userCode} AND orleDepartId IN(");
		boolean bl = false;
		for(Long orleDptId : roleDptIds){
			if(bl){sbSql.append(",");}
			sbSql.append(orleDptId);
			if(!bl){bl=!bl;}
		}
		sbSql.append(")");
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		roleDptIds.clear();
		return content;
		
	}
	
	//新增特殊菜单权限
	public String addOrguserSpecilSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Set<Long> menus = (Set<Long>)params.get("menus");//菜单
		int type = (Integer)params.get("type");
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("INSERT INTO xbdun_power_user_spec(userCode,orgCode,code,type) VALUES");
		boolean bl = false;
		for(Long menu : menus){
			if(bl){sbSql.append(",");}
			sbSql.append("(#{userCode},#{orgCode},"+menu+","+type+")");
			if(!bl){bl=!bl;}
		}
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
//		menus.clear();
		return content;
	}
	
	//删除特殊菜单
	public String delOrgUserSpecilSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Set<Long> menus = (Set<Long>)params.get("menus");//菜单
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("DELETE FROM xbdun_power_user_spec WHERE orgCode=#{orgCode} AND userCode = #{userCode} AND code IN (");
		boolean bl = false;
		for(Long menu : menus){
			if(bl){sbSql.append(",");}
			sbSql.append(menu);
			if(!bl){bl=!bl;}
		}
		sbSql.append(")");
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
}
